package com.dx.mes.service.special.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dx.mes.common.transcoder.util.Util;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_line_wo;
import com.dx.mes.common.dao.IRetLineWoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.facade.special.service.IMsplinewoService;
import com.dx.mes.facade.special.tx.Msplinewo.MsplinewoI;
import com.dx.mes.facade.special.tx.Msplinewo.MsplinewoO;
import com.dx.mes.facade.special.tx.Msplinewo.MsplinewoOA;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("msplinewoService")
@SuppressWarnings({"unchecked"})
public class MsplinewoService implements IMsplinewoService{
	private LogUtils logUtils;
	private static String TX_NAME = T_MSPMILEWO;

	@Autowired
	private IRetLineWoDao retLineWoDao;

	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(MsplinewoService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(MsplinewoService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");

		Session session = retLineWoDao.getCrSession();
		MsplinewoO outTrx = new MsplinewoO();
		String strOutTrx = null;

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		//输入对象
		try {
			MsplinewoI inTrx = new MsplinewoI();
			//将json字符串转换成对象
			inTrx = JacksonUtil.fromJson(strInTrx, MsplinewoI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);

			switch (action_flg) {
				case 'Q':
					queryByCondition(inTrx,outTrx);
					break;
				case 'A': {
					saveFunc(inTrx, outTrx);
					break;
				}
				case 'D': {
					delFunc(inTrx, outTrx);
					break;
				}
				case 'S': {
					activeFunc(inTrx, outTrx);
					break;
				}
				default:
					errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(Util.stackTraceToString(ex));
			logUtils.error(Util.stackTraceToString(ex));
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			//将一个 Object 或者List对象转化为JSOObject或者JSONArray
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}

	//查询设定
	public void queryByCondition(MsplinewoI inTrx ,MsplinewoO outTrx){
		List<MsplinewoOA> oaryList = new ArrayList<MsplinewoOA>();
		String hql = "FROM Ret_line_wo WHERE 1=1";
		if (!Util.isSpaceCheck(inTrx.getLine_id())) {
			hql += " and line_id_fk='" + inTrx.getLine_id() +"'";
		}
		if (!Util.isSpaceCheck(inTrx.getOpe_id())) {
			hql += " and ope_id_fk='" + inTrx.getOpe_id()+ "'";
		}
		if (!Util.isSpaceCheck(inTrx.getWo_id())) {
			hql += " and wo_id_fk='" + inTrx.getWo_id() + "'";
		}
		if (!Util.isSpaceCheck(inTrx.getValid_flg())) {
			hql += " and valid_flg='" + inTrx.getValid_flg() + "'";
		}	
		List<Ret_line_wo> line_woList = retLineWoDao.find(hql);
		for(Ret_line_wo line_wo:line_woList){
			MsplinewoOA oary = new MsplinewoOA();
			oary.setRpt_timestamp(line_wo.getRpt_timestamp().toString());
			oary.setLine_id(line_wo.getLine_id_fk());
			oary.setOpe_id(line_wo.getOpe_id_fk());
			oary.setWo_id(line_wo.getWo_id_fk());
			oary.setValid_flg(line_wo.getValid_flg());
			oary.setValid_usr(line_wo.getValid_usr());
			oary.setInvalid_usr(line_wo.getInvalid_usr());
			oary.setValid_timestamp(line_wo.getValid_timestamp());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	//保存设定信息
	public void saveFunc(MsplinewoI inTrx ,MsplinewoO outTrx) {
		//检查信息
		if (Util.isSpaceCheck(inTrx.getLine_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("线别信息不能为空");
			return;
		}
		if (Util.isSpaceCheck(inTrx.getOpe_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("站点信息不能为空");
			return;
		}
		if (Util.isSpaceCheck(inTrx.getWo_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("工单信息不能为空");
			return;
		}
		String hql = "FROM Ret_line_wo WHERE line_id_fk='" + inTrx.getLine_id() 
				+ "' and ope_id_fk='" + inTrx.getOpe_id() 
				+ "' and wo_id_fk='" + inTrx.getWo_id() + "'";
		List<Ret_line_wo> line_woList = retLineWoDao.find(hql);
		if(line_woList.size()>0){
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("该线别[" + inTrx.getLine_id() 
					+ "],站点[" + inTrx.getOpe_id() 
					+ "],工单[" + inTrx.getWo_id() 
					+"]信息已经设定!");
			return;
		}
		Ret_line_wo ret_line_wo = new Ret_line_wo();
		ret_line_wo.setRpt_timestamp(Util.getCurrentTimestamp());
		ret_line_wo.setLine_id_fk(inTrx.getLine_id());
		ret_line_wo.setWo_id_fk(inTrx.getWo_id());
		ret_line_wo.setOpe_id_fk(inTrx.getOpe_id());
		ret_line_wo.setValid_flg(_NO);

		retLineWoDao.save(ret_line_wo);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	//删除设定
	public void delFunc(MsplinewoI inTrx ,MsplinewoO outTrx) {
		//检查信息
		if (Util.isSpaceCheck(inTrx.getLine_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("线别信息不能为空");
			return;
		}
		if (Util.isSpaceCheck(inTrx.getOpe_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("站点信息不能为空");
			return;
		}
		if (Util.isSpaceCheck(inTrx.getWo_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("工单信息不能为空");
			return;
		}
		String hql = "FROM Ret_line_wo WHERE line_id_fk='" + inTrx.getLine_id() 
				+ "' and ope_id_fk='" + inTrx.getOpe_id() 
				+ "' and wo_id_fk='" + inTrx.getWo_id() + "'";
		List<Ret_line_wo> line_woList = retLineWoDao.find(hql);
		if(line_woList.size() > 0){
			retLineWoDao.deleteAll(line_woList);
		}
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	public void activeFunc(MsplinewoI inTrx ,MsplinewoO outTrx) {
		//检查信息
		if (Util.isSpaceCheck(inTrx.getLine_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("线别信息不能为空");
			return;
		}
		if (Util.isSpaceCheck(inTrx.getOpe_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("站点信息不能为空");
			return;
		}
		if (Util.isSpaceCheck(inTrx.getWo_id())) {
			outTrx.setRtn_code(E_MSPMILEWO_INVALID_INPUT+"");
			outTrx.setRtn_mesg("工单信息不能为空");
			return;
		}
		String hql = "FROM Ret_line_wo WHERE line_id_fk='" + inTrx.getLine_id() 
				+ "' and ope_id_fk='" + inTrx.getOpe_id() 
				+ "' and wo_id_fk='" + inTrx.getWo_id() + "'";
		List<Ret_line_wo> line_woList = retLineWoDao.find(hql);
		if(line_woList.size()==0){
			outTrx.setRtn_code(E_RET_LINE_WO + E_READ_NOT_FOUND +"");
			outTrx.setRtn_mesg("需要激活的信息不存在!");
			return;
		}
		
		Ret_line_wo ret_line_wo = line_woList.get(0);
		if (_NO.equals(ret_line_wo.getValid_flg())){
			String hqlM = "FROM Ret_line_wo WHERE valid_flg='Y' and line_id_fk='" + inTrx.getLine_id() 
				+ "' and ope_id_fk='" + inTrx.getOpe_id() + "'";
			//hqlM += " and wo_id_fk ='" + ret_line_wo.getWo_id_fk() +"'";
			List<Ret_line_wo> line_wos = retLineWoDao.find(hqlM);
			if(line_wos.size()>0){
				outTrx.setRtn_code(E_MSPMILEWO_INVALID_STATUS+"");
				outTrx.setRtn_mesg("线别[" + inTrx.getLine_id() 
						+ "],站点[" + inTrx.getOpe_id()
						+ "],工单[" + line_wos.get(0).getWo_id_fk() + "]已经被激活，请先关闭");
				return;
			}
			ret_line_wo.setValid_flg(_YES);
			ret_line_wo.setValid_timestamp(Util.getCurrentTimestamp());
			ret_line_wo.setValid_usr(inTrx.getEvt_usr());
			retLineWoDao.update(ret_line_wo);
		}else {
			ret_line_wo.setValid_flg(_NO);
			ret_line_wo.setInvalid_usr(inTrx.getEvt_usr());
			retLineWoDao.update(ret_line_wo);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	
	//错误信息
	public void errActionFnc(MsplinewoO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}

